<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      function deepCopy(obj) {
        // 判断是否是对象
        if (typeof obj === "object") {
          // 执行深拷贝
          let res = Array.isArray(obj) ? {} : [];
          for (let key in obj) {
            let item = obj[key];
            res[key] = typeof item === "object" ? deepCopy(item) : item;
          }
          return res;
        } else {
          return obj;
        }
      }
      const a = [
        {
          field1: 1,
          field2: undefined,
          field3: {
            child: "child",
          },
          field4: [2, 4, 8],
        },
      ];
      let b = deepCopy(a);
      console.log(a, b);
      b[0].field3.child = "child变了";
      console.log(a, b);
    </script>
  </body>
</html>
